
/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController;

import com.dc.gameserver.proto.gameserverprotoVo.Level;
import com.dc.gameserver.proto.gameserverprotoVo.LevelRequest;
import com.dc.gameserver.proto.gameserverprotoVo.LevelResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TPointPrograss;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 石头哥哥
 *         Project : LandlordsServer
 *         Date: 13-6-4
 *         Time: 上午11:49
 *         Connect: 13638363871@163.com
 *         关卡类型加载
 */
@Service
@SuppressWarnings("unchecked")
public class levelController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;
    /**
     */
    @Override
    public void PostConstruct() {
        int ID= LevelRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]= LevelRequest.getDefaultInstance();
    }

     /**
     * messageLite数据结构体分发
     *
     * @param messageLite
     * @param player
     * @throws Exception
      *
      * 返回玩家区域信息
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        LevelRequest levelRequest= (LevelRequest) messageLite;
        int       areaID=levelRequest.getAreaID();     //请求区域编号
        byte     LevelType= (byte) levelRequest.getLevelType(); //关卡类型 :1：普通 2：神识 3：隐藏
        List<TPointPrograss> levelList=(List<TPointPrograss>) modelCacheService.
                        findListByPrefixID(ModelCacheService.POINT_PROGRASS_PREFIX(player.getID()));
        List<TPointPrograss> willSendList=new FastTable<TPointPrograss>();
        //推送请求关卡类型数据
        for (TPointPrograss level:levelList){
            if (level.getBelongareaid()==areaID&&level.getPointtype()==LevelType){
                willSendList.add(level);
            }
        }
        LevelResponse.Builder builder=LevelResponse.newBuilder();
        if (willSendList.size()==0){ //no data
            willSendList.clear();
            willSendList=null;
            builder.addAllLevelList(new ArrayList<Level>());
            player.DirectSend(wrappedBufferShort(LevelResponse.msgID.ID_VALUE,builder.build()));
            return;
        }

        for (TPointPrograss level:willSendList)  {
            Level.Builder levelBuilder=Level.newBuilder();
            levelBuilder.setLevelID(level.getPointid()); //关卡编号
            levelBuilder.setCompletePerDay(level.getPointpercompletenum()); //每天完成的次数  针对  普通，神识关卡 ；
            levelBuilder.setCanPlayTimes(level.getPointlasttimes());//隐藏关卡可玩剩余次数
            levelBuilder.setStarNum(level.getPointstarnum());// 星级数量
            levelBuilder.setLevelStatus(level.getPointstatus());//关卡完成状态  1:未完成;2:完成
            builder.addLevelList(levelBuilder.build());
        }
        player.DirectSend(wrappedBufferShort(LevelResponse.msgID.ID_VALUE,builder.build()));
        willSendList.clear();
        willSendList=null;
    }
}
